#include <iostream>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

LL f[N], g[N];

LL qpow(LL a,LL b, LL p)
{
	int ret = 1;
	while(b)
	{
		if(b & 1) ret = (ret * a) % p;
		b >>= 1;
		a = (a * a) % p;
	}
	return ret;
}

void init(int p)
{
	int n = p - 1;
	f[0] = 1;
	for(int i = 1; i <= n; i++) f[i] = f[i - 1] * i % p;
	
	g[n] = qpow(f[n], p - 2, p);
	for(int i = n - 1; i >= 0; i--) g[i] = g[i + 1] * (i + 1) % p;
}

LL get_c(int n, int m, int p)
{
	if(m > n) return 0;
	return f[n] * g[m] % p * g[n - m] % p;
}

int lucas(int n, int m, int p)
{
	if(m == 0) return 1;
	return (lucas(n / p, m / p, p) * get_c(n % p, m % p, p)) % p;
}

int main()
{
	int T; cin >> T;
	while(T--)
	{
		int n, m, p; cin >> n >> m >> p;
		init(p);
		cout << lucas(n + m, m, p) << endl;
	}
	return 0;
}
